home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0987.arc / ALSPRO.TST next >
Encoding:
Text File  |  1985-07-12  |  14.6 KB  |  615 lines

  1. %
  2. %  "Translations" of Prolog benchmarks first used on Turbo Prolog.
  3. %
  4. %  When running these benchmarks, remember that the read/1 predicate expects
  5. %  to see a period to signal the end of input!
  6. %
  7. %  Note:  All errors from the original set of benchmarks have been fixed.
  8. %
  9. %                                       a.lane (4/17/87)
  10. %
  11. %---------------------------------------------------------------------
  12.  
  13. %   Factorial Benchmark Test
  14.  
  15. fact :-
  16.   write('Enter number of iterations '),
  17.   read(Iter),nl,
  18.   write('Enter factorial number '),
  19.   read(Num),nl,nl,
  20.   time(Start),
  21.   repeat(Iter,Num),
  22.   time(Finish),nl, Overall is Finish - Start,
  23.   write('Time is '),write(Overall),nl.
  24.  
  25.   factorial(1,1) :- !.
  26.   factorial(N,Result) :-
  27.      N1 is N - 1,
  28.      factorial(N1, Temporary),
  29.      Result is N * Temporary.
  30.  
  31.   repeat(0,R) :-  write(X),nl.
  32.   repeat(N,R) :-  factorial(R,_),
  33.                   N1 is N - 1,
  34.                   repeat(N1,R).
  35.  
  36.   time(Time) :- Time is cputime.
  37.  
  38. %---------------------------------------------------------------------
  39.  
  40. %   List Reversal Test Program
  41.  
  42. lrev :-
  43.   write('Enter cycle length '),
  44.   read(N),
  45.   time(Start),
  46.   cycle(N),
  47.   time(Finish), Overall is Finish - Start,
  48.   write('Time = '),write(Overall),nl.
  49.  
  50. append( [], L, L ).
  51. append( [Z|L1], L2, [Z|L3] ) :- append( L1, L2, L3 ).
  52.  
  53.  
  54. lips(L) :- rev( [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
  55. 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,
  56. 43,44,45,46,47,48,49,50], L ).
  57.  
  58. lipshort(L) :- rev( [1,2,3,4,5,6,7,8,9,10], L ).
  59.  
  60.  
  61. rev( [], [] ).
  62. rev( [H|T], L ) :- rev( T,Z), append( Z, [H], L ).
  63.  
  64. cycle(0).
  65. cycle(N) :-  N1 is N - 1, lips(_), cycle(N1).
  66.  
  67. time(Time) :- Time is cputime.
  68.  
  69. %---------------------------------------------------------------------
  70.  
  71. %   Floating Point Test Program
  72.  
  73. %  This floating-point benchmark is significantly different from the one
  74. %  performed on the Turbo Prolog.
  75. %
  76. %  The heart of the Turbo benchmark reads:
  77. %
  78. %   calc(A,B) :-
  79. %      C is 1.0,
  80. %      C1 is C * A,
  81. %      C2 is C1 * B,
  82. %      C3 is C2 / A,
  83. %      C  is C3 / B,
  84. %      bound(C).
  85. %
  86. %  which is flawed for two reasons.  First, the value of C is reinitialized each
  87. %  time the predicate calc/2 is called, which defeats one of the reasons for
  88. %  performing these operations 5000 times:  To see if there is any cumulative
  89. %  error.  Second, the Turbo benchmark works only if the result of C3 / B
  90. %  is *exactly* 1.0.  If there is any error (i.e., if, as is true with ALS
  91. %  Prolog, C3 / B is 1.00000000000000000001) the thing won't fly at all.
  92.  
  93. float :-
  94.   time(Start),
  95.   cycle(5000, 1.0, 2.71828, 3.14159 ),
  96.   time(Finish), Overall is Finish - Start,
  97.   write('Time is '),write(Overall),nl.
  98.  
  99.   calc(In,Out,A,B) :-
  100.      C1 is In * A,
  101.      C2 is C1 * B,
  102.      C3 is C2 / A,
  103.      Out is C3 / B.
  104.  
  105.   cycle(0,C,A,B) :-
  106.      write('C is ',C),nl.
  107.  
  108.  
  109.   cycle(N,C,A,B) :-
  110.      calc(C,CF,A,B),
  111.      N1 is N - 1,
  112.      cycle(N1,CF,A,B).
  113.  
  114. time(Time) :- Time is cputime.
  115.  
  116. %---------------------------------------------------------------------
  117.  
  118. %   Math Functions Test Program
  119.  
  120. goal :-
  121.  write('Doing square root...'),nl,
  122.  time(T1),
  123.  cyclesqrt(1000,_,T1),
  124.  write('Doing logs..'),nl,
  125.  time(T2),
  126.  cycleln(1000,_,T2),
  127.  write('Doing exp..'),nl,
  128.  time(T3),
  129.  cycleexp(1000,_,T3),
  130.  write('Doing atan..'),nl,
  131.  time(T4),
  132.  cycleatan(1000,_,T4),
  133.  write('Doing sin...'),nl,
  134.  time(T5),
  135.  cyclesin(1000,_,T5).
  136.  
  137. cyclesqrt(0,R,T1) :- time(T6),  Stime is T6 - T1,
  138.                   write('SQRT : '),write(Stime),nl,!.
  139.  
  140. cyclesqrt(N, R, T) :-
  141.     N > 0, N1 is N - 1, R is sqrt(100.0), cyclesqrt(N1,R,T).
  142.  
  143. cycleln(0,R,T2) :- time(T7), Ltime is T7 - T2,
  144.                 write('LN   : '),write(Ltime),nl.
  145.  
  146. cycleln(N, R,T) :-
  147.     N > 0, N1 is N - 1, R is log(100.0), cycleln(N1,R,T).
  148.  
  149. cycleexp(0,R,T3) :- time(T8), Etime is T8 - T3,
  150.                  write('EXP  : '),write(Etime),nl.
  151.  
  152. cycleexp(N, R,T) :-
  153.     N > 0, N1 is N - 1, R is exp(10.0), cycleexp(N1,R,T).
  154.  
  155. cycleatan(0,R,T4) :- time(T9),  Atime is T9 - T4,
  156.                   write('ATAN : '),write(Atime),nl.
  157.  
  158. cycleatan(N, R,T) :-
  159.     N > 0, N1 is N - 1, R is atan(10.0), cycleatan(N1,R,T).
  160.  
  161. cyclesin(0,R,T5) :- time(T10),  Stime is T10 - T5,
  162.                  write('SIN  : '),write(Stime),nl.
  163.  
  164. cyclesin(N, R,T) :-
  165.     N > 0, N1 is N - 1, R is sin(10.0), cyclesin(N1,R,T).
  166.  
  167. time(Time) :- Time is cputime.
  168.  
  169. %---------------------------------------------------------------------
  170.  
  171. %   Disk Read Program
  172.  
  173. dread :-
  174.   see('a:tempo.dat'),
  175.   time(Start),
  176.   get_text(512),
  177.   time(Finish), Overall is Finish - Start,
  178.   seen, see(user),
  179.   write('Time = '),write(Overall),nl,
  180.   write('DONE'),nl.
  181.  
  182. get_text(0).
  183. get_text(N) :-
  184.      read(Str),
  185.      N1 is N - 1,
  186.      get_text(N1).
  187.  
  188. %---------------------------------------------------------------------
  189.  
  190. %   Disk write benchmark
  191.  
  192. dwrite :-
  193.   tell('a:tempo.dat'),
  194.   time(Start),
  195.   send_text(512),
  196.   time(Finish), Overall is Finish - Start,
  197.   told, tell(user),
  198.   write('Time = '),write(Overall),nl,
  199.   write('DONE'),nl.
  200.  
  201. send_text(0).
  202. send_text(N) :-
  203.      write('x2345678123456781234567812345670123456781234567812345678123456701234
  204. 56781234567812345678123456701234567812345678123456781234567.'),
  205.      nl, N1 is N - 1,
  206.      send_text(N1).
  207.  
  208. time(Time) :- Time is cputime.
  209.  
  210. %---------------------------------------------------------------------
  211.  
  212. %   Tower of Hanoi Test Program
  213.  
  214. hanoi :-
  215.   write('Enter tower height '),
  216.   read(High),
  217.   time(Start),
  218.   hanoi(High),
  219.   time(Finish), Overall is Finish - Start,
  220.   write('Time : '),write(Overall),nl.
  221.  
  222.  
  223. hanoi(N) :- move(N, left, center, right).
  224.  
  225. move(0, _, _, _) :- !.
  226. move(N, A, B, C) :-
  227.      M is N - 1,
  228.      move(M, A, C, B),
  229.      move2(bottom, A, B),
  230.      move(M, C, B, A).
  231. move2(bottom, A, B) :-
  232.      write('Move the disk on '),
  233.      write(A),
  234.      write(' to '),
  235.      write(B),
  236.      nl.
  237.  
  238. time(Time) :- Time is cputime.
  239.  
  240. %---------------------------------------------------------------------
  241.  
  242. %   Sieve test program
  243.  
  244. erasthones :-
  245.   time(Start),
  246.   cycle(10),
  247.   time(Finish), Overall is Finish - Start,
  248.   write('Time is '),write(Overall),nl.
  249.  
  250. primes( Limit, Ps ) :-
  251.         integers( 2, Limit, Is ),
  252.         sift( Is, Ps ).
  253.  
  254. integers( Low, High, [Low|Rest] ) :-
  255.         Low =< High, !, M is Low + 1,
  256.         integers(M, High, Rest ).
  257. integers( _,_,[] ).
  258.  
  259. sift( [], [] ).
  260. sift( [I|Is], [I|Ps]) :-
  261.         remove(I,Is,New),
  262.         sift( New, Ps ).
  263.  
  264. remove(_,[],[]).
  265. remove(P,[I|Is],[I|Nis]) :-
  266.         I mod P =\= 0,!,
  267.         remove(P, Is, Nis).
  268. remove(P,[I|Is],Nis) :-
  269.         I mod P =:= 0,
  270.         remove(P, Is, Nis).
  271.  
  272. cycle(0).
  273. cycle(N) :-
  274.         N1 is N - 1,
  275.         primes(100,_),
  276.         cycle(N1).
  277.  
  278. time(Time) :- Time is cputime.
  279.  
  280. %---------------------------------------------------------------------
  281.  
  282. %  This program is called with the query "?-boresea(X)."
  283. %  X is the number of loop iterations executed. It should be big
  284. %  enough to give significant results.
  285. %  suggested value for X: 100 for interpreted code
  286. %                        1000 for compiled code
  287. %  average values for C-prolog interpreter:
  288. %        X=1000, Tloop=27.1 T.comp=1.0 Tnet=26.1 Klips=7.7
  289.  
  290. boresea(X)
  291.      :- T1 is cputime,
  292.         do_max_KLips(X),       %  calls the loop to execute the
  293.         T2 is cputime,         %  sequence of 200 predicates
  294.         compens_loop(X),       %  compensation loop
  295.         T3 is cputime,
  296.         print_times(T1,T2,T3,X,200). % compute and print results
  297.  
  298.  
  299. compens_loop(0).                    %  compensation loop
  300. compens_loop(X) :- Y is X - 1, compens_loop(Y).
  301.  
  302. print_times(T1,T2,T3,X,I) :-        %  prints the results
  303.         TT1 is T2 - T1,
  304.         TT2 is T3 - T2,
  305.         TT is TT1 - TT2,
  306.         write('T overall loop:   '),write(TT1), nl,
  307.         write('T compens loop:   '),write(TT2), nl,
  308.         write('T net:            '),write(TT),nl,
  309.         write('KLips:            '),
  310.         Li is I * X,
  311.         Lips is Li / TT,
  312.         KLips is Lips / 1000,
  313.         write(KLips),nl,nl.
  314.  
  315. do_max_KLips(0).            %  loop calling the actual benchmark
  316. do_max_KLips(X) :- lips1, Y is X - 1, do_max_KLips(Y).
  317.  
  318. %  predicates to test call
  319.  
  320. lips1 :- lips2.
  321. lips2 :- lips3.
  322. lips3 :- lips4.
  323. lips4 :- lips5.
  324. lips5 :- lips6.
  325. lips6 :- lips7.
  326. lips7 :- lips8.
  327. lips8 :- lips9.
  328. lips9 :- lips10.
  329. lips10 :- lips11.
  330. lips11 :- lips12.
  331. lips12 :- lips13.
  332. lips13 :- lips14.
  333. lips14 :- lips15.
  334. lips15 :- lips16.
  335. lips16 :- lips17.
  336. lips17 :- lips18.
  337. lips18 :- lips19.
  338. lips19 :- lips20.
  339. lips20 :- lips21.
  340. lips21 :- lips22.
  341. lips22 :- lips23.
  342. lips23 :- lips24.
  343. lips24 :- lips25.
  344. lips25 :- lips26.
  345. lips26 :- lips27.
  346. lips27 :- lips28.
  347. lips28 :- lips29.
  348. lips29 :- lips30.
  349. lips30 :- lips31.
  350. lips31 :- lips32.
  351. lips32 :- lips33.
  352. lips33 :- lips34.
  353. lips34 :- lips35.
  354. lips35 :- lips36.
  355. lips36 :- lips37.
  356. lips37 :- lips38.
  357. lips38 :- lips39.
  358. lips39 :- lips40.
  359. lips40 :- lips41.
  360. lips41 :- lips42.
  361. lips42 :- lips43.
  362. lips43 :- lips44.
  363. lips44 :- lips45.
  364. lips45 :- lips46.
  365. lips46 :- lips47.
  366. lips47 :- lips48.
  367. lips48 :- lips49.
  368. lips49 :- lips50.
  369. lips50 :- lips51.
  370. lips51 :- lips52.
  371. lips52 :- lips53.
  372. lips53 :- lips54.
  373. lips54 :- lips55.
  374. lips55 :- lips56.
  375. lips56 :- lips57.
  376. lips57 :- lips58.
  377. lips58 :- lips59.
  378. lips59 :- lips60.
  379. lips60 :- lips61.
  380. lips61 :- lips62.
  381. lips62 :- lips63.
  382. lips63 :- lips64.
  383. lips64 :- lips65.
  384. lips65 :- lips66.
  385. lips66 :- lips67.
  386. lips67 :- lips68.
  387. lips68 :- lips69.
  388. lips69 :- lips70.
  389. lips70 :- lips71.
  390. lips71 :- lips72.
  391. lips72 :- lips73.
  392. lips73 :- lips74.
  393. lips74 :- lips75.
  394. lips75 :- lips76.
  395. lips76 :- lips77.
  396. lips77 :- lips78.
  397. lips78 :- lips79.
  398. lips79 :- lips80.
  399. lips80 :- lips81.
  400. lips81 :- lips82.
  401. lips82 :- lips83.
  402. lips83 :- lips84.
  403. lips84 :- lips85.
  404. lips85 :- lips86.
  405. lips86 :- lips87.
  406. lips87 :- lips88.
  407. lips88 :- lips89.
  408. lips89 :- lips90.
  409. lips90 :- lips91.
  410. lips91 :- lips92.
  411. lips92 :- lips93.
  412. lips93 :- lips94.
  413. lips94 :- lips95.
  414. lips95 :- lips96.
  415. lips96 :- lips97.
  416. lips97 :- lips98.
  417. lips98 :- lips99.
  418. lips99 :- lips100.
  419. lips100:- lips101.
  420. lips101 :- lips102.
  421. lips102 :- lips103.
  422. lips103 :- lips104.
  423. lips104 :- lips105.
  424. lips105 :- lips106.
  425. lips106 :- lips107.
  426. lips107 :- lips108.
  427. lips108 :- lips109.
  428. lips109 :- lips110.
  429. lips110 :- lips111.
  430. lips111 :- lips112.
  431. lips112 :- lips113.
  432. lips113 :- lips114.
  433. lips114 :- lips115.
  434. lips115 :- lips116.
  435. lips116 :- lips117.
  436. lips117 :- lips118.
  437. lips118 :- lips119.
  438. lips119 :- lips120.
  439. lips120 :- lips121.
  440. lips121 :- lips122.
  441. lips122 :- lips123.
  442. lips123 :- lips124.
  443. lips124 :- lips125.
  444. lips125 :- lips126.
  445. lips126 :- lips127.
  446. lips127 :- lips128.
  447. lips128 :- lips129.
  448. lips129 :- lips130.
  449. lips130 :- lips131.
  450. lips131 :- lips132.
  451. lips132 :- lips133.
  452. lips133 :- lips134.
  453. lips134 :- lips135.
  454. lips135 :- lips136.
  455. lips136 :- lips137.
  456. lips137 :- lips138.
  457. lips138 :- lips139.
  458. lips139 :- lips140.
  459. lips140 :- lips141.
  460. lips141 :- lips142.
  461. lips142 :- lips143.
  462. lips143 :- lips144.
  463. lips144 :- lips145.
  464. lips145 :- lips146.
  465. lips146 :- lips147.
  466. lips147 :- lips148.
  467. lips148 :- lips149.
  468. lips149 :- lips150.
  469. lips150 :- lips151.
  470. lips151 :- lips152.
  471. lips152 :- lips153.
  472. lips153 :- lips154.
  473. lips154 :- lips155.
  474. lips155 :- lips156.
  475. lips156 :- lips157.
  476. lips157 :- lips158.
  477. lips158 :- lips159.
  478. lips159 :- lips160.
  479. lips160 :- lips161.
  480. lips161 :- lips162.
  481. lips162 :- lips163.
  482. lips163 :- lips164.
  483. lips164 :- lips165.
  484. lips165 :- lips166.
  485. lips166 :- lips167.
  486. lips167 :- lips168.
  487. lips168 :- lips169.
  488. lips169 :- lips170.
  489. lips170 :- lips171.
  490. lips171 :- lips172.
  491. lips172 :- lips173.
  492. lips173 :- lips174.
  493. lips174 :- lips175.
  494. lips175 :- lips176.
  495. lips176 :- lips177.
  496. lips177 :- lips178.
  497. lips178 :- lips179.
  498. lips179 :- lips180.
  499. lips180 :- lips181.
  500. lips181 :- lips182.
  501. lips182 :- lips183.
  502. lips183 :- lips184.
  503. lips184 :- lips185.
  504. lips185 :- lips186.
  505. lips186 :- lips187.
  506. lips187 :- lips188.
  507. lips188 :- lips189.
  508. lips189 :- lips190.
  509. lips190 :- lips191.
  510. lips191 :- lips192.
  511. lips192 :- lips193.
  512. lips193 :- lips194.
  513. lips194 :- lips195.
  514. lips195 :- lips196.
  515. lips196 :- lips197.
  516. lips197 :- lips198.
  517. lips198 :- lips199.
  518. lips199 :- lips200.
  519. lips200.
  520.  
  521. %---------------------------------------------------------------------
  522. %  choice point benchmark.
  523.  
  524. %  The predicates are called:
  525. %     o  "choice_point(N)"    - creation of choice points
  526. %   N is the number of loop iterations executed
  527.  
  528. %  predicate to test creation of choice points without
  529. %  backtracking
  530.  
  531. choice_point(N) :- T1 is cputime,
  532.         cre_CP(N), T2 is cputime,
  533.         compens_loop(N), T3 is cputime,
  534.         print_times(T1,T2,T3,N,20).
  535.  
  536. %  compensation loop, used to measure the time spent in
  537. %  the loop
  538.  
  539. compens_loop(0).
  540. compens_loop(X) :- Y is X - 1, compens_loop(Y).
  541.  
  542. %  loop to test choice point creation
  543.  
  544. cre_CP(0).
  545. cre_CP(N):-M is N-1, ccp1(0,0,0), cre_CP(M).
  546.  
  547. cre_CP0ar(0).
  548. cre_CP0ar(N):-M is N-1, ccp1, cre_CP0ar(M).
  549.  
  550.  
  551.  
  552. print_times(T1,T2,T3,X,I) :-        %  prints the results
  553.         TT1 is T2 - T1,
  554.         TT2 is T3 - T2,
  555.         TT is TT1 - TT2,
  556.         write('T overall loop:   '),write(TT1), nl,
  557.         write('T compens loop:   '),write(TT2), nl,
  558.         write('T net:            '),write(TT),nl,
  559.         write('KLips:            '),
  560.         Li is I * X,
  561.         Lips is Li / TT,
  562.         KLips is Lips / 1000,
  563.         write(KLips),nl,nl.
  564.  
  565. %  ccp1 creates 20 choice points
  566. %  ccp1 is the beginning of a set of predicates
  567. %  composed of 2 clauses each. Every invokation of nd0 will create
  568. %  a sequence of 20 choice points. The body of the clauses are
  569. %  limited to one goal, thus avoiding a creation of environment
  570. %  when the clause is activated. nd0, and its successors, have
  571. %  three arguments to comply with our average static analysis
  572. %  results made on more than 30 real Prolog programs.
  573. %  ccpXX exists with 3 arguments, and 0 args.
  574.  
  575. ccp1(X,Y,Z):-ccp2(X,Y,Z).
  576. ccp1(X,Y,Z).
  577. ccp2(X,Y,Z):-ccp3(X,Y,Z).
  578. ccp2(X,Y,Z).
  579. ccp3(X,Y,Z):-ccp4(X,Y,Z).
  580. ccp3(X,Y,Z).
  581. ccp4(X,Y,Z):-ccp5(X,Y,Z).
  582. ccp4(X,Y,Z).
  583. ccp5(X,Y,Z):-ccp6(X,Y,Z).
  584. ccp5(X,Y,Z).
  585. ccp6(X,Y,Z):-ccp7(X,Y,Z).
  586. ccp6(X,Y,Z).
  587. ccp7(X,Y,Z):-ccp8(X,Y,Z).
  588. ccp7(X,Y,Z).
  589. ccp8(X,Y,Z):-ccp9(X,Y,Z).
  590. ccp8(X,Y,Z).
  591. ccp9(X,Y,Z):-ccp10(X,Y,Z).
  592. ccp9(X,Y,Z).
  593. ccp10(X,Y,Z):-ccp11(X,Y,Z).
  594. ccp10(X,Y,Z).
  595. ccp11(X,Y,Z):-ccp12(X,Y,Z).
  596. ccp11(X,Y,Z).
  597. ccp12(X,Y,Z):-ccp13(X,Y,Z).
  598. ccp12(X,Y,Z).
  599. ccp13(X,Y,Z):-ccp14(X,Y,Z).
  600. ccp13(X,Y,Z).
  601. ccp14(X,Y,Z):-ccp15(X,Y,Z).
  602. ccp14(X,Y,Z).
  603. ccp15(X,Y,Z):-ccp16(X,Y,Z).
  604. ccp15(X,Y,Z).
  605. ccp16(X,Y,Z):-ccp17(X,Y,Z).
  606. ccp16(X,Y,Z).
  607. ccp17(X,Y,Z):-ccp18(X,Y,Z).
  608. ccp17(X,Y,Z).
  609. ccp18(X,Y,Z):-ccp19(X,Y,Z).
  610. ccp18(X,Y,Z).
  611. ccp19(X,Y,Z):-ccp20(X,Y,Z).
  612. ccp19(X,Y,Z).
  613. ccp20(X,Y,Z).
  614. ccp20(X,Y,Z).
  615.